import { request } from "@/utils/https";

interface ResponseData<T> {
  code: number;
  message: string;
  data: T;
}
export interface LoginData {
  userAccount: string;
  userPassword: string;
  checkPassword?: string;
}

// 后端返回的数据
interface LoginResponseData {
  createTime: string;
  email: string;
  gender: string;
  id: string;
  phone: string;
  updateTime: string;
  userAccount: string;
  userAvatar: string;
  userName: string;
  userProfile: string;
  userRole: string;
}
const login = (data: LoginData) => {
  return request<ResponseData<LoginResponseData>>(
    "/api/user/login",
    "POST",
    data,
    {},
  );
};

/**
 * 用户注册
 */
export interface RegisterData {
  userAccount: string;
  userPassword: string;
  checkPassword: string;
}

interface RegisterResponseData {
  id: string;
}

const register = (data: RegisterData) => {
  return request<ResponseData<RegisterResponseData>>(
    "/api/user/register",
    "POST",
    data,
    {},
  );
};

// 根据id查询信息

// 后端返回的数据
interface UserInfoResponseData {
  createTime: string; 
  email: string;
  gender: string;
  id: string;
  phone: string;
  integral: number; // 积分
  userAccount: string;
  userAvatar: string;
  userName: string;
  userProfile: string;
  userRole: string;
}
export const getUserInfo = (id: string) => {
  return request<ResponseData<UserInfoResponseData>>(
    "/api/user/get/vo",
    "GET",
    { id },
    {},
  );
};

/**
 * 文件上传
 */
interface UploadResponseData {
  url: string;
  filename: string;
}

const uploadFile = (data: any) =>
  request<ResponseData<UploadResponseData>>(
    "/api/sys/common/upload",
    "POST",
    data,
    {},
  );

/**
 * 获取二维码地址
 */
const getQrCodeUrl = () => {
  return request<ResponseData<string>>(
    "/api/systemConfig/value/qrCodeUrl",
    "get",
    {},
    {},
  );
};

/**
 * 问题建议相关接口
 */
export interface QuestionSuggestionAddRequest {
  userId: number;
  content: string;
}

export interface QuestionSuggestionResponse {
  id: number;
  userId: number;
  content: string;
  status: number;
  createTime: string;
  updateTime: string;
}

/**
 * 提交问题建议
 */
const submitQuestionSuggestion = (data: QuestionSuggestionAddRequest) => {
  return request<ResponseData<number>>(
    "/api/questionSuggestion/add",
    "POST",
    data,
    {},
  );
};

/**
 * 获取用户的问题建议列表
 */
const getUserQuestionSuggestions = (userId: number) => {
  return request<ResponseData<QuestionSuggestionResponse[]>>(
    `/api/questionSuggestion/user/${userId}`,
    "GET",
    {},
    {},
  );
};

export { login, uploadFile, register, getQrCodeUrl, submitQuestionSuggestion, getUserQuestionSuggestions };
